﻿using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Linq;
using System;
using System.Diagnostics;

namespace KouDai.Metro.Controls
{
    public class AnimatedWrapPanel : Panel
    {
        #region Fields

        //private double[] _columnsX;      //存储列高度的数组
        private int _columnCount;       //列的数量
        private double _columnMargin;   //边距
        private double _viewHeight;         //高度

        #endregion

        #region Constructor
        public AnimatedWrapPanel()
        {
            //WrapGrid
            //OrientedVirtualizingPanel
            //VirtualizingPanel
            //IScrollSnapPointsInfo
        }
        #endregion

        #region Properties

        public int ColumnCount
        {
            get { return _columnCount; }
            set { _columnCount = value; }
        }

        public double ColumnMargin
        {
            get { return _columnMargin; }
            set { _columnMargin = value; }
        }

        public double ViewHeight
        {
            get { return _viewHeight; }
            set { _viewHeight = value; }
        }

        #endregion

        #region Overrride
        protected override Size MeasureOverride(Size availableSize)
        {
            Size infiniteSize = new Size(double.PositiveInfinity, double.PositiveInfinity);

            double[] measureColumnX = new double[_columnCount];

            foreach (UIElement child in Children)
            {
                child.Measure(infiniteSize);
                int lowestIndex = GetLowestIndex(measureColumnX);
                measureColumnX[lowestIndex] += child.DesiredSize.Width + _columnMargin;
            }

            Size resultSize = new Size();

            resultSize.Width = double.IsPositiveInfinity(availableSize.Width) ? measureColumnX.Min() : availableSize.Width;
            resultSize.Height = double.IsPositiveInfinity(availableSize.Height) ? _viewHeight : availableSize.Height;

            return resultSize;
        }
        #endregion

        #region ArrangeOverride

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (this.Children == null || this.Children.Count == 0)
            {
                return finalSize;
            }

            double[] arrangeColumnX = new double[_columnCount];

            foreach (UIElement child in Children)
            {
                int lowestIndex = GetLowestIndex(arrangeColumnX);
                double averageHeight = GetAverageHeight();

                double y = (_columnMargin + averageHeight) * lowestIndex;
                double x = arrangeColumnX[lowestIndex] + _columnMargin - 10;

                arrangeColumnX[lowestIndex] = x + child.DesiredSize.Width;

                child.Arrange(new Rect(x,y,child.DesiredSize.Width,child.DesiredSize.Height));
            }

            return finalSize;
        }

        #endregion

        #region Method
        private int GetLowestIndex(double[] columnX)
        {
            if (columnX == null)
            {
                throw new ArgumentNullException("_columns");
            }

            int lowestIndex = 0;
            for (int i = 0; i < _columnCount; i++)
            {
                if (columnX[lowestIndex] > columnX[i])
                {
                    lowestIndex = i;
                }
            }
            return lowestIndex;
        }
        
        private double GetAverageHeight()
        {
            return (_viewHeight - (_columnCount - 1) * _columnMargin) / _columnCount;
        }
        #endregion
    }
}
